<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/13
 * Time: 14:39
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Talks extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where['t_status'] = ['<>',3];
            if(isset($user_name) && $user_name != ''){
                $where['t_content'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($username) && $username != ''){
                $where['user_username|user_phone'] = ['like', '%' . $username . '%'];
            }
            if(isset($t_status) && $t_status != ''){
                $where['t_status'] = $t_status;
            }
            if(isset($t_fid) && $t_fid != ''){
                $where['t_fid'] = $t_fid;
            }
            if(isset($t_type) && $t_type != ''){
                $where['t_type'] = $t_type;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_talks t')
                ->join('up_u_user u', 't.t_user_id = u.user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $field = 't.*,user_username, user_phone, user_auth';
            $lists = Db::table('up_talks t')
                ->join('up_u_user u', 't.t_user_id = u.user_id', 'left')
                ->field($field)
                ->where($where)
                ->page($NowPage,$limits)
                ->order('t_status asc,t_update desc')
                ->select();
            if($lists){
                foreach ($lists as $k => $v){
                   if($v['t_type'] == 1){
                       if($v['t_image']){
                           $lists[$k]['t_image'] = explode(',',$v['t_image']);
                       }else{
                           $lists[$k]['t_image'] = [];
                       }
                   }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function newAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'new_fid' => $new_fid,
                'new_title' => $new_title,
                'new_thumb' => $new_thumb,
                'new_sign_left' => $new_sign_left,
                'new_sign_right' => $new_sign_right,
                'new_video' => $new_video,
                'new_status' => $new_status,
                'new_time' => date('Y-m-d H:i:s'),
                'new_update' => date('Y-m-d H:i:s')
            ];
            if($new_fid == 1){
                if(!$new_type1){
                    return json(['code' => 100, 'msg' => '请选择分类']);
                }
                $data['new_type'] = $new_type1;
            }else{
                if(!$new_type2){
                    return json(['code' => 100, 'msg' => '请选择分类']);
                }
                $data['new_type'] = $new_type2;
            }
            if($new_video == 1){
                $data['new_content'] = $new_content;
            }else{
                if(!$video){
                    return json(['code' => 100, 'msg' => '请上传视频']);
                }
                $data['new_content'] = $video;
            }
            $res = Db::table('up_news')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $types1 = Db::name('news_type')
            ->where('nt_fid',1)
            ->field('nt_id,nt_name,nt_fid')
            ->order('nt_sort desc')
            ->select();
        $types2 = Db::name('news_type')
            ->where('nt_fid',2)
            ->field('nt_id,nt_name,nt_fid')
            ->order('nt_sort desc')
            ->select();
        $this->assign([
            'types1' => $types1,
            'types2' => $types2,
        ]);
        return $this->fetch('news/newadd');
    }
    /**
     * 编辑
     * @return mixed
     */
    public function newEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'new_fid' => $new_fid,
                'new_title' => $new_title,
                'new_thumb' => $new_thumb,
                'new_sign_left' => $new_sign_left,
                'new_sign_right' => $new_sign_right,
                'new_video' => $new_video,
                'new_status' => $new_status,
                'new_time' => date('Y-m-d H:i:s'),
                'new_update' => date('Y-m-d H:i:s')
            ];
            if($new_fid == 1){
                if(!$new_type1){
                    return json(['code' => 100, 'msg' => '请选择分类']);
                }
                $data['new_type'] = $new_type1;
            }else{
                if(!$new_type2){
                    return json(['code' => 100, 'msg' => '请选择分类']);
                }
                $data['new_type'] = $new_type2;
            }
            if($new_video == 1){
                $data['new_content'] = $new_content;
            }else{
                if(!$video){
                    return json(['code' => 100, 'msg' => '请上传视频']);
                }
                $data['new_content'] = $video;
            }
            $res = Db::table('up_news')->where('new_id',$new_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $new_id = input('new_id');
        $info = Db::name('news')
            ->where('new_id',$new_id)
            ->find();
        $types1 = Db::name('news_type')
            ->where('nt_fid',1)
            ->field('nt_id,nt_name,nt_fid')
            ->order('nt_sort desc')
            ->select();
        $types2 = Db::name('news_type')
            ->where('nt_fid',2)
            ->field('nt_id,nt_name,nt_fid')
            ->order('nt_sort desc')
            ->select();
        $this->assign([
            'types1' => $types1,
            'types2' => $types2,
            'info' => $info,
        ]);
        return $this->fetch('news/newedit');
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function newDel(){
        $new_id = input('id');
        $res = Db::table('up_talks')->where('t_id',$new_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 启用
     * @return \think\response\Json
     */
    public function newUp(){
        $new_id = input('id');
        $res = Db::table('up_talks')->where('t_id',$new_id)->setField('t_status',1);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 冻结
     * @return \think\response\Json
     */
    public function newDown(){
        $new_id = input('id');
        $res = Db::table('up_talks')->where('t_id',$new_id)->setField('t_status',2);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }

    //-------------------------------------------------评论回复------------------------------------------------
    /**
     * 评论
     * @return mixed|\think\response\Json
     */
    public function dynComment(){
        if(request()->isAjax()){
            $user_name = input('user_name');
            $where['comment_type'] = 2;
            $where['comment_new_id'] = session('new_id');
            if(isset($user_name) && $user_name != ''){
                $where['comment_content'] = ['like', '%' . $user_name . '%'];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_comment')
                ->join('up_u_user', 'user_id = comment_reply_user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_comment')
                ->join('up_u_user', 'user_id = comment_reply_user_id', 'left')
                ->field('comment_id, comment_content, comment_time,  comment_zan, user_username')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('comment_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $new_id = input('new_id');
        session('new_id', $new_id);
        return $this->fetch('talks/dyncomment');
    }

    /**
     * 删除评论
     * @return \think\response\Json
     */
    public function commentDel(){
        $comment_id = input('id');
        $dyn_id = Db::table('up_comment')->where('comment_id', $comment_id)->value('comment_new_id');
        Db::startTrans();
        try{
            // 删除评论
            Db::table('up_comment')->delete($comment_id);
            // 帖子评论数减一
            Db::table('up_talks')->where('t_id', $dyn_id)->setDec('t_comment');

            Db::commit();
            return json(['code' => 200, 'msg' => '删除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

    /**
     * 回复
     * @return mixed|\think\response\Json
     */
    public function commentReply(){
        if(request()->isAjax()){
            $user_name = input('user_name');
            $where['comtt_comment_id'] = session('comment_id');
            if(isset($user_name) && $user_name != ''){
                $where['comtt_content'] = ['like', '%' . $user_name . '%'];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $field = 'u.user_id one_id,u2.user_id reply_id,u.user_username one_name,u2.user_username reply_name';
            $field .= ',m.*';
            $count = Db::table('up_comment_tt')->alias('m')
                ->join('up_u_user u','m.comtt_one_user_id=u.user_id','LEFT')
                ->join('up_u_user u2','m.comtt_reply_user_id=u2.user_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_comment_tt')->alias('m')
                ->join('up_u_user u','m.comtt_one_user_id=u.user_id','LEFT')
                ->join('up_u_user u2','m.comtt_reply_user_id=u2.user_id','LEFT')
                ->field($field)
                ->where($where)
                ->page($NowPage,$limits)
                ->order('comtt_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $comment_id = input('comment_id');
        session('comment_id', $comment_id);
        return $this->fetch('talks/commentreply');
    }

    /**
     * 删除回复
     * @return \think\response\Json
     */
    public function comttDel(){
        $comtt_id = input('id');
        $res = Db::table('up_comment_tt')->delete($comtt_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}